package com.china08.yxyapi.api.manage.sch.platform.users.teacher;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.lessons.YxyLessonsSchoolLesson;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.YxyLessonsSchoolLessonRepository;
import com.china08.yxyapi.security.profile.YxyProfile;

import io.swagger.annotations.ApiOperation;

@CrossOrigin
@RestController
public class YxyLessonsTeachingSchoolLessonsCrtController extends YxyApiBaseController {

    @Autowired
    YxyLessonsSchoolLessonRepository lessonRepository;

    @ApiOperation("")
    @RequestMapping(value = "/api_yw/manage/tec/lessons/tea/school/create.e", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel process(HttpServletRequest request, HttpServletResponse response, @RequestBody ReqModel reqModel) {
        YxyProfile yxyProfile = getProfile(request, response);
        String schoolId = yxyProfile.getSchoolId();
        String author = yxyProfile.getUserNick();
        String catalogId1 = reqModel.catalogId1;
        String catalogId2 = reqModel.catalogId2;
        String name = reqModel.name;
        String summary = reqModel.summary;
        String cover = reqModel.cover;
        Integer vip = reqModel.vip;
        Integer orderNum = lessonRepository.countBySchoolId(schoolId) + 1;

        YxyLessonsSchoolLesson schoolLesson = new YxyLessonsSchoolLesson();
        schoolLesson.setCatalogId1(catalogId1);
        schoolLesson.setCatalogId2(catalogId2);
        schoolLesson.setName(name);
        schoolLesson.setSummary(summary);
        schoolLesson.setCover(cover);
        schoolLesson.setVip(vip);
        schoolLesson.setCount(0);
        schoolLesson.setDuration(0);
        schoolLesson.setCreateDate(new Date());
        schoolLesson.setStatus(0);
        schoolLesson.setPv(0);
        schoolLesson.setVv(0);
        schoolLesson.setIsVisible(0);
        schoolLesson.setSchoolId(schoolId);
        schoolLesson.setAuthor(author);
        schoolLesson.setOrderNum(orderNum);
        schoolLesson = lessonRepository.save(schoolLesson);

        RespModel respModel = new RespModel();
        respModel.lessonId = schoolLesson.getId();
        return respModel;

    }

    public static class ReqModel extends YxyApiReqModel {
        public String catalogId1;
        public String catalogId2;
        public String cover;
        public String name;
        public String summary;
        public Integer vip;

        @Override
        public void autoCheck() {
            Assert.isTrue(StringUtils.isNotBlank(catalogId1), "请选择一级分类名称");
            Assert.isTrue(StringUtils.isNotBlank(catalogId2), "请选择二级分类名称");
            Assert.isTrue(StringUtils.isNotBlank(name), "课程名称不能为空");
            Assert.isTrue(name.length() > 0 && name.length() < 36, "请输入1-35个的课程名称");
            Assert.isTrue(StringUtils.isNotBlank(summary), "课程简介不能为空");
            Assert.isTrue(summary.length() > 0 && summary.length() < 501, "请输入1-500字的简介");
            Assert.isTrue(StringUtils.isNotBlank(cover), "课程封面不能为空");
        }
    }

    public static class RespModel extends YxyApiRespModel {
        public String lessonId;
    }
}
